package com.tgy.leetcode;

import java.util.Arrays;

/**
 * @Author: tgy
 * @Date: 1/20/21 9:50 AM
 *
 *  https://leetcode-cn.com/problems/maximum-product-of-three-numbers/
 */
public class _628_三个数的最大乘积 {

    public int maximumProduct(int[] nums) {

        int smallFirst = Integer.MAX_VALUE;
        int smalSecond = smallFirst;

        int largeFirst = Integer.MIN_VALUE;
        int largeSecond = largeFirst;
        int largeThree = largeSecond;

        for (int num : nums) {
          //

            if (num < smallFirst) {

                smalSecond = smallFirst;
                smallFirst = num;
            }else if (num < smalSecond) {

                smalSecond = num;
            }

            if (num > largeFirst) {

                largeThree = largeSecond;
                largeSecond = largeFirst;
                largeFirst = num;
            } else if (num > largeSecond) {

                largeThree = largeSecond;
                largeSecond = num;
            }else if (num > largeThree) {

                largeThree = num;
            }

        }
    System.out.println(
        smallFirst + ":" + smalSecond + ":" + largeFirst + ":" + largeSecond + ":" + largeThree);
        return Math.max(smallFirst * smalSecond * largeFirst,largeFirst*largeSecond*largeThree);
    }

    public int maximumProduct01(int[] nums) {

        Arrays.sort(nums);
        int len = nums.length;
        return Math.max(nums[0]*nums[1]*nums[len - 1],nums[len-3]*nums[len-2]*nums[len-1]);
    }

  public static void main(String[] args) {
    //
      _628_三个数的最大乘积 maximumProduct = new _628_三个数的最大乘积();

//      maximumProduct.maximumProduct(new int[]{-100,-98,-1,2,3,4});
      //[1,2,3,2]
      maximumProduct.maximumProduct(new int[]{1,2,3,2});
  }
}
